#include "mypixmapitem.h"
#include<QDebug>
MyPixMapItem::MyPixMapItem(QObject *parent) : QObject(parent)
{
 En_X=false;
 En_Y=true;
}
MyPixMapItem::MyPixMapItem(QString filename,QGraphicsItem *parent):QGraphicsItem(parent),En_X(false),En_Y(true)
{
  if(!filename.isEmpty())
      pix.load(filename);
      outpix=pix;
      update();
}

QRectF MyPixMapItem::boundingRect() const
{
     return QRectF(-pix.width()/2,-pix.height()/2,pix.width(),pix.height());
}

void MyPixMapItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
  //  Q_UNUSED(painter);
    Q_UNUSED(option);
    Q_UNUSED(widget);
    painter->drawPixmap(boundingRect().topLeft(),outpix);
    //qDebug()<<"painter event";
}

void MyPixMapItem::setXrate(float xrate)
{
    int deltax;
    float rate;
    rate=xrate;
    if(rate>100.0) rate=100.0;
    if(rate<=0.0){ rate=0.0;deltax=1;}
    else
    deltax=(int)(((float)pix.width())*xrate/100.0);
    if(En_X)
             outpix=pix.copy(pix.width()-deltax,0,pix.width(),pix.height());
    update();
}

void MyPixMapItem::setXrate_ltr(float xrate)
{
    int deltax;
    float rate;
    rate=xrate;
    if(rate>100.0) rate=100.0;
    if(rate<=0.0){ rate=0.0;deltax=1;}
    else
    deltax=(int)(((float)pix.width())*xrate/100.0);
    if(En_X)
             outpix=pix.copy(pix.width()-deltax,0,pix.width(),pix.height());
    update();
}

void MyPixMapItem::setYrate(float yrate)
 {
        int deltay;
        float rate;
        rate=yrate;
        if(rate>100.0) rate=100.0;
        if(rate<=0.0){ rate=0.0;deltay=1;}
        else
        deltay=(int)(((float)pix.height())*yrate/100.0);
        if(En_Y)
                outpix=pix.copy(0,pix.height()-deltay,pix.width(),pix.height());
        update();
}
void MyPixMapItem::setPixmap(QString filename)
{
     pix.load(filename);
}

void MyPixMapItem::setXEn(bool xen)
{
    En_X=xen;
}
void MyPixMapItem::setYEn(bool yen)
{
    En_Y=yen;
}

void MyPixMapItem:: mousePressEvent(QGraphicsSceneMouseEvent *event)
{

}
void  MyPixMapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{

}
void  MyPixMapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{

}
